package com.kmxd.ams.core.common.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.exception.BizException;
import java.util.Arrays;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 出入库类型
 *
 * @author nickbi
 */
@Getter
@AllArgsConstructor
public enum RegistrationInAndOutEnum {
  /** 出库 */
  OUT_OF_WAREHOUSE("1", "出库"),
  STORAGE("2", "入库"),
  BORROW_OUT("3", "借阅出库"),
  ;

  private String code;
  private String desc;

  public static RegistrationInAndOutEnum convert(String type) {
    return Arrays.stream(values())
        .filter(x -> x.code.equals(type))
        .findAny()
        .orElseThrow(() -> BizException.instance(ErrorCode.NOT_FOUND, "未找到出入库类型：" + type));
  }

  @JsonCreator
  public static RegistrationInAndOutEnum getValue(String type) {
    return Arrays.stream(values()).filter(x -> Objects.equals(x.code, type)).findAny().orElse(null);
  }
}
